package com.example.peibei.ui.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

import com.example.peibei.R;
import com.flyco.tablayout.SlidingTabLayout;

import java.lang.reflect.Field;

public class MySlidingTabLayout extends SlidingTabLayout {
    Drawable drawable;

    public MySlidingTabLayout(Context context) {
        super(context);
    }

    public MySlidingTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySlidingTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setDrawable(Drawable drawable) {
        this.drawable = drawable;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            Field field = getClass().getSuperclass().getDeclaredField("mIndicatorDrawable");
            field.setAccessible(true);
            field.set(this,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            super.onDraw(canvas);
        } catch (Exception e) {
        }
        if (drawable != null){
            try {
                Field field2 = getClass().getSuperclass().getDeclaredField("mIndicatorRect");
                Field field = getClass().getSuperclass().getDeclaredField("mIndicatorHeight");
                field.setAccessible(true);
                field2.setAccessible(true);
                Rect mIndicatorRect = (Rect) field2.get(this);
                float mIndicatorHeight = (float) field.get(this);
                int height = getHeight();
                drawable.setBounds(mIndicatorRect.left,
                        height - (int) mIndicatorHeight,
                        mIndicatorRect.right,
                        height);
                drawable.draw(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            Field field = getClass().getSuperclass().getDeclaredField("mIndicatorDrawable");
            field.setAccessible(true);
            field.set(this,drawable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
